#!/usr/bin/python
#   yongming.li
from pychartdir import *
from Myconfig import *
from ProMonitor import *
import copy


class CPUChart:
    def __init__(self):
        self.version = '0.1'
  
    def showmultiline(self,picname,value):

        # The data for the line chart
        #data0 = copy.deepcopy(value[0])
        #data1 = copy.deepcopy(value[1])
        #data2 =copy.deepcopy(value[2])

        # The labels for the line chart
        labels = []
        for i in range(0,Myconfig.samplecount):
            labels.append('%s'%(i))

        # Create an XYChart object of size 600 x 300 pixels, with a light blue (EEEEFF)
        # background, black border, 1 pxiel 3D border effect and rounded corners
        c = XYChart(Myconfig.simplelinepointwidth, Myconfig.multilinepointheigth, 0xeeeeff, 0x000000, 1)
        c.setRoundedFrame()

        # Set the plotarea at (55, 58) and of size 520 x 195 pixels, with white background.
        # Turn on both horizontal and vertical grid lines with light grey color (0xcccccc)
        c.setPlotArea(45, 30, Myconfig.simplelinepointwidth-50, Myconfig.multilinepointheigth-10, 0xffffff, -1, -1, 0xcccccc, 0xcccccc)

        # Add a legend box at (50, 30) (top of the chart) with horizontal layout. Use 9 pts
        # Arial Bold font. Set the background and border color to Transparent.
        c.addLegend(45, 5, 0, "arialbd.ttf", 9).setBackground(Transparent)

        # Add a title box to the chart using 15 pts Times Bold Italic font, on a light blue
        # (CCCCFF) background with glass effect. white (0xffffff) on a dark red (0x800000)
        # background, with a 1 pixel 3D border.
        #c.addTitle("Application", "timesbi.ttf", 15).setBackground(0xccccff, 0x000000, glassEffect())

        # Add a title to the y axis
        c.yAxis().setTitle("MBytes")

        # Set the labels on the x axis.
        c.xAxis().setLabels(labels)

        # Display 1 out of 3 labels on the x-axis.
        c.xAxis().setLabelStep(3)

        # Add a title to the x axis
        c.xAxis().setTitle("Jun 12, 2006")

        # Add a line layer to the chart
        layer = c.addLineLayer2()

        # Set the default line width to 2 pixels
        layer.setLineWidth(2)

        # Add the three data sets to the line layer. For demo purpose, we use a dash line
        # color for the last line
        color = [0xff0000]
        #print  value.monitorcontainer
        for process in value.namelist:
            name = process[0]
            color = process[1]
            if value.monitorcontainer.has_key(name):
                temp= copy.deepcopy(value.monitorcontainer[name])
                layer.addDataSet(temp, color, name)
        
        #layer.addDataSet(data1, 0x008800, "Server #2")
        #layer.addDataSet(data2, c.dashLineColor(0x3333ff, DashLine), "Server #3")

        # Output the chart
        c.makeChart('./out/'+picname+"multiline.png")


    def showsimpleline(self,name,value):
        # The data for the line chart
        #data = [30, 28, 40, 55, 75, 68, 54, 60, 50, 62, 75, 65, 75, 91, 60, 55, 53, 35, 50,
        #      66, 56, 48, 52, 65, 62]
        data = copy.deepcopy(value)

        # The labels for the line chart
        labels = []
        for i in range(0,Myconfig.samplecount):
            labels.append('%s'%(i))

        # Create a XYChart object of size 250 x 250 pixels
        c = XYChart(Myconfig.simplelinepointwidth, Myconfig.simplelinepointheigth)

        # Set the plotarea at (30, 20) and of size 200 x 200 pixels
        c.setPlotArea(30, 20, Myconfig.simplelinepointwidth-20, Myconfig.simplelinepointheigth-20)

        # Add a line chart layer using the given data
        c.addLineLayer(data)

        # Set the labels on the x axis.
        c.xAxis().setLabels(labels)

        # Display 1 out of 3 labels on the x-axis.
        c.xAxis().setLabelStep(3)

        # Output the chart
        c.makeChart('./out/'+name+'simpleline.png')
    def showsimplearea(self,name,value):

        data = copy.deepcopy(value)
        # The labels for the area chart
        
        labels = []
        for i in range(0,Myconfig.samplecount):
            labels.append('%s'%(i))
        
        labelsy = ["0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"]

        for i in range(0,100):
            labelsy.append('%s'%(i))

        # Create a XYChart object of size 250 x 250 pixels
        c = XYChart(Myconfig.simplelinepointwidth, Myconfig.simplelinepointheigth)

        # Set the plotarea at (30, 20) and of size 200 x 200 pixels
        c.setPlotArea(40, 20, Myconfig.simplelinepointwidth-20, Myconfig.simplelinepointheigth-50)

        # Add an area chart layer using the given data
        c.addAreaLayer(data)

        # Set the labels on the x axis.
        c.xAxis().setLabels(labels)
        #c.yAxis().setLabels(labelsy)

        # Display 1 out of 3 labels on the x-axis.
        c.xAxis().setLabelStep(5)
        #c.yAxis().setLabelStep(10)

        # Output the chart
        c.makeChart('./out/'+name+'simplearea.png')
         
    def showenhancedarea(self,name,value):
        # The data for the area chart
        data = copy.deepcopy(value)

        # The labels for the area chart
        labels = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",\
               "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24"]

        # Create a XYChart object of size 600 x 360 pixels, with a brushed silver background,
        # 1 pixel 3D border effect, rounded corners and soft drop shadow.
        c = XYChart(Myconfig.simplelinepointwidth, Myconfig.simplelinepointheigth, brushedSilverColor(), Transparent, 1)
        c.setRoundedFrame()
        c.setDropShadow()

        # Add a title box to the chart using 18 pts Times Bold Italic font.
        title = c.addTitle("<*block,valign=absmiddle*><*img=star.png*><*img=star.png*> Performance " \
                         "Enhancer <*img=star.png*><*img=star.png*><*/*>", "timesbi.ttf", 18)

        #
        # Use a text box with a depressed 3D border to create the inner depressed region
        #

        # The width of the frame border
        frameWidth = 5

        # Set the depressed region position
        contentBox = c.addText(frameWidth, title.getHeight(), "")
        contentBox.setSize(c.getDrawArea().getWidth() - 1 - frameWidth * 2, \
                         c.getDrawArea().getHeight() - title.getHeight() - frameWidth - 1)

        # Use -1 as the rasied effect to create a depressed region
        contentBox.setBackground(Transparent, Transparent, -1)

        # Set rounded corners, and put the text box at the back of the chart
        contentBox.setRoundedCorners(10)
        contentBox.setZOrder(ChartBackZ)

        # Tentatively set the plotarea to 50 pixels from the left depressed edge, and 25
        # pixels under the top depressed edge. Set the width to 75 pixels less than the
        # depressed region width, and the height to 75 pixels less than the depressed region
        # height. Use white (ffffff) background, transparent border, and grey (cccccc)
        # horizontal and vertical grid lines.
        plotArea = c.setPlotArea(50 + contentBox.getLeftX(), contentBox.getTopY() + 25,
                              contentBox.getWidth() - 75, contentBox.getHeight() - 75, 0xffffff, -1, -1,0xcccccc, -1)

        # Add a title to the y axis
        #c.yAxis().setTitle("Energy Concentration (KJ per liter)")

        # Set the labels on the x axis.
        c.xAxis().setLabels(labels)

        # Display 1 out of 3 labels on the x-axis.
        c.xAxis().setLabelStep(3)

        # Add a title to the x axis using CDML
        #c.xAxis().setTitle("<*block,valign=absmiddle*><*img=clock.png*>  Elapsed Time (hour)<*/*>")

        # Set the axes width to 2 pixels
        c.xAxis().setWidth(2)
        c.yAxis().setWidth(2)

        # Add an area layer to the chart using a gradient color that changes vertically from
        # semi-transparent red (60ff0000) to semi-transparent white (60ffffff)
        c.addAreaLayer(data, c.linearGradientColor(0, contentBox.getTopY() + 20, 0, \
                      contentBox.getTopY() + contentBox.getHeight() - 50, 0x60ff0000, 0x60ffffff))

        # Adjust the plot area size, such that the bounding box (inclusive of axes) is 15
        # pixels from the left depressed edge, 25 pixels below the top depressed edge, 25
        # pixels from the right depressed edge, and 15 pixels above the bottom depressed
        # edge.
        c.packPlotArea(contentBox.getLeftX() + 15, contentBox.getTopY() + 25,
                     contentBox.getLeftX() + contentBox.getWidth() - 25, 
                     contentBox.getTopY() + contentBox.getHeight() - 15)

        # Add a custom CDML text with the bottom right corner is anchored to the bootom right
        # corner of the plot area, with 5 pixels margin.
        c.addText(plotArea.getLeftX() + plotArea.getWidth() - 5, plotArea.getTopY() + plotArea.getHeight() - 5, \
                     "<*block,valign=absmiddle*><*img=small_molecule.png*> <*block*>" \
                     "<*font=timesbi.ttf,size=10,color=804040*>Molecular\nEngineering<*/*>").setAlignment(BottomRight)
        # Output the chart
        c.makeChart(name+"enhancedarea.png")

    def showroundmeter(self,name,cpuvalue):
        # The value to display on the meter
        
        value = copy.deepcopy(cpuvalue)

        # Create an AugularMeter object of size 200 x 200 pixels, with silver background,
        # black border, 2 pixel 3D depressed border and rounded corners.
        m = AngularMeter(200, 200, silverColor(), 0x000000, -2)
        m.setRoundedFrame()

        # Set the meter center at (100, 100), with radius 85 pixels, and span from -135 to
        # +135 degress
        m.setMeter(100, 100, 85, -135, 135)

        # Meter scale is 0 - 100, with major tick every 10 units, minor tick every 5 units,
        # and micro tick every 1 units
        m.setScale(0, 100, 10, 5, 1)

        # Disable default angular arc by setting its width to 0. Set 2 pixels line width for
        # major tick, and 1 pixel line width for minor ticks.
        m.setLineWidth(0, 2, 1)

        # Set the circular meter surface as metallic blue (9999DD)
        m.addRing(0, 90, metalColor(0x9999dd))

        # Add a blue (6666FF) ring between radii 88 - 90 as decoration
        m.addRing(88, 90, 0x6666ff)

        # Set 0 - 60 as green (99FF99) zone, 60 - 80 as yellow (FFFF00) zone, and 80 - 100 as
        # red (FF3333) zone
        m.addZone(0, 60, 0x99ff99)
        m.addZone(60, 80, 0xffff00)
        m.addZone(80, 100, 0xff3333)

        # Add a text label centered at (100, 135) with 15 pts Arial Bold font
        m.addText(100, 135, name, "arialbd.ttf", 15, TextColor, Center)

        # Add a text box centered at (100, 165) showing the value formatted to 2 decimal
        # places, using white text on a black background, and with 1 pixel 3D depressed
        # border
        m.addText(100, 165, m.formatValue(value, "2"), "arial.ttf", 8, 0xffffff, Center).setBackground(0x000000, 0x000000, -1)

        # Add a semi-transparent blue (40333399) pointer at the specified value
        m.addPointer(value, 0x40333399)

        # Output the chart
        m.makeChart('./out/'+name+"roundmeter.png")


